<?php
require('config.php');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
	    <!-- Meta, title, CSS, favicons, etc. -->
	    <meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="description" content="pureCrawler by thaodx@gmail.com.">
		<meta name="keywords" content="crawler, pureCrawler, content grabber">
		<meta name="author" content="ThaoDX">

		<title>pureCrawler</title>
		<!-- Latest compiled and minified CSS -->
		<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
		<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
		<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
		<!-- Optional theme -->
		<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
		<!-- Latest compiled and minified JavaScript -->
		<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
		<style type="text/css">
		body {
			padding-top: 70px;
		}
		#modal-body{
			max-height: 400px;
			overflow: auto;
		}
		.smallfont{
			font-size:11px !important;
			font-family: courier;
		}
		.extract-detail{
			cursor: pointer;
		}
		</style>
		<script type="text/javascript">
		var startUrl, filterUrl, filterString, filterDetail, filterContentString, limitUrl;
		$(document).ready(function(){

			$('#btn-save').click(function(){
				id = $('#hdn_id').val();
				startUrl = $('#home_url').val();

				filterUrl = $('#filter_url').val();
				filterString = $('#filter_string').val();
				urlAdd = $('#link_before').val();
				
				filterDetail = $('#filter_detail').val();
				filterContentString = $('#filter_content_string').val();

				limitUrl = $('#limit_url').val();
				name = $('#name').val();
				removeDom = $('#dom_remove').val();

				oldHtml = $('#btn-save').html();
				$('#btn-save').html('Saving...');

				$.ajax({
					url: 'add.php',
					dataType: 'json',
					type: 'POST',
					data: {id:id, startUrl:startUrl, filterUrl:filterUrl, urlAdd:urlAdd, filterString:filterString, filterDetail:filterDetail, filterContentString:filterContentString, limitUrl:limitUrl, name:name, removeDom:removeDom},
					success: function(rs){
						if(typeof rs != 'undefined' && rs == 1){
							alert('Config saved');
							location.href = 'index.php';
						}
						else{
							alert('Error when saving data.');
						}
						$('#btn-save').html(oldHtml);
					}
				});

				return false;
			});

			$('#btn-checkcfg').click(function(){
				startUrl = $('#home_url').val();

				filterUrl = $('#filter_url').val();
				filterString = $('#filter_string').val();
				
				filterDetail = $('#filter_detail').val();
				filterContentString = $('#filter_content_string').val();
				removeDom = $('#dom_remove').val();
				limitUrl = $('#limit_url').val();
				urlAdd = $('#link_before').val();

				$('#modal-title').html('Check grab config..');
				$('#modal-body').html('<img src="images/loader.gif" id="loader" />');

				$.ajax({
					url: 'checkcfg.php',
					dataType: 'json',
					type: 'POST',
					data: {startUrl:startUrl, filterUrl:filterUrl, filterString:filterString, filterDetail:filterDetail, filterContentString:filterContentString, limitUrl:limitUrl, urlAdd:urlAdd,removeDom:removeDom},
					success: function(rs){
						if(typeof rs.data != 'undefined' && rs.data.length > 0){
							var html = '';
							for(i = 0, l = rs.data.length; i < l; i++){
								html += '<tr><td>' + rs.data[i] + '</td></tr>';
							}
							$('#modal-body').html('<table class="smallfont table table-bordered table-hover">' + html + '</table><hr />');
							if(typeof rs.detail != 'undefined'){
								$('#modal-body').append('<div class="well">' + rs.detail + '</div>');
							}
						}
					},
					complete: function(){
						
					},
					error: function(xhr){
						alert(xhr)
					}
				});
			});
		});
		</script>
	</head>
	<body>
		<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
		  <div class="container-fluid">
		    <!-- Brand and toggle get grouped for better mobile display -->
		    <div class="navbar-header">
		      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
		        <span class="sr-only">Toggle navigation</span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		      </button>
		      <a class="navbar-brand" href="index.php"><span class="glyphicon glyphicon-home"></span> Home</a>
		    </div>
		  </div><!-- /.container-fluid -->
		</nav>

		<div class="container-fluid">
			<div class="row">
				<div class="col-md-4">
					<div class="panel panel-success">
					  <!-- Default panel contents -->
					  <div class="panel-heading">Saved config</div>
					  <!-- Table -->
					  <table class="table table-hover">
					    <tr>
					    	<th>Config name</th>
					    	<th width="50"></th>
					    </tr>
					    <?php
					    $sqlSite = "SELECT * FROM sites ORDER BY id DESC";
					    $rsSite = mysql_query($sqlSite);
					    while($row = mysql_fetch_array($rsSite)):
					    ?>
					    <tr>
					    	<td><?php echo $row['name'] ?>[<?php echo $row['id'] ?>]</td>
					    	<td><a title="Edit config" href="?id=<?php echo $row['id']; ?>" class="edit-config" data-id="<?php echo $row['id'] ?>"><span class="glyphicon glyphicon-edit"></span></a> <a href="parse.php?id=<?php echo $row['id'] ?>" title="View data"><span class="glyphicon glyphicon-share"></span></a></td>
					    </tr>
						<?php endwhile; ?>
					  </table>
					</div>
				</div>
				<div class="col-md-8">
					<?php
                    $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
                    $sqlConfig = "SELECT * FROM sites WHERE id = $id";
                    $rsConfig = mysql_query($sqlConfig);
                    $rowConfig = mysql_fetch_assoc($rsConfig);
                    // echo '<pre>'; print_r($rowConfig); echo '</pre>';
                    ?>
					<!-- Primary content -->
					<div class="panel panel-success">
					  <!-- Default panel contents -->
					  <div class="panel-heading">Add new grabber site</div>
					  <!-- Form -->
					  <div class="container-fluid" style="margin:10px 0;">
						<form class="form-horizontal" role="form">
							<?php if($rowConfig['id']): ?>
	                          <div class="form-group">
							    <label for="name" class="col-sm-4 control-label">ID</label>
							    <div class="col-sm-8">
								  <input type="text" class="form-control" style="width:100px;" id="hdn_id" name="hdn_id" value="<?php echo $rowConfig['id']; ?>" />
							    </div>
							  </div>
							<?php endif; ?>
                          <div class="form-group">
						    <label for="name" class="col-sm-4 control-label">Name</label>
						    <div class="col-sm-8">
						      <input type="text" name="name" class="form-control" id="name" value="<?php echo $rowConfig['name']; ?>" placeholder="Name of config">
						    </div>
						  </div>

						  <div class="form-group">
						    <label for="home_url" class="col-sm-4 control-label">Starting URL</label>
						    <div class="col-sm-8">
						      <input type="text" class="form-control" id="home_url" value="<?php echo $rowConfig['home_url']; ?>" placeholder="Example: http://google.com">
						    </div>
						  </div>
						  <hr />
						  <!-- Filter URL -->
						  <div class="form-group">
						    <label for="filter_url" class="col-sm-4 control-label">Filter URL by</label>
						    <div class="col-sm-8">
						      <select id="filter_url" id="filter_url" class="form-control" style="width: 200px;">
						      	<option value="">None</option>
						      	<option value="RSS" <?php echo ($rowConfig['link_filter_type'] == 'RSS') ? 'selected' : '' ?>>From RSS feed</option>
						      	<option value="HASWORD" <?php echo ($rowConfig['link_filter_type'] == 'HASWORD') ? 'selected' : '' ?>>Has string</option>
						      	<option value="REGX" <?php echo ($rowConfig['link_filter_type'] == 'REGX') ? 'selected' : '' ?>>Regular Expression</option>
						      	<option value="DOM" <?php echo ($rowConfig['link_filter_type'] == 'DOM') ? 'selected' : '' ?>>DOM
						      </select>
						    </div>
						  </div>
					      <div class="form-group" id="frm-filter-url">
					    	<label for="filter_string" class="col-sm-4 control-label">Filter string</label>
						    <div class="col-sm-8">
						      <input type="text" class="form-control" id="filter_string" value="<?php echo htmlentities(base64_decode($rowConfig['link_filter_string'])); ?>" placeholder="DOM: div#content, Regx: <tag>(.*?)</tag>">
						    </div>
					      </div>
						  <!-- URL add -->
						  <div class="form-group">
						    <label for="link_before" class="col-sm-4 control-label">Add before link</label>
						    <div class="col-sm-8">
						     <input type="text" class="form-control" id="link_before" value="<?php echo $rowConfig['link_before']; ?>" placeholder="Example: http://domain.com/">
						    </div>
						  </div>

					      <!--/ Filter URL -->

						  <hr />
						  <!-- Filter Detail -->
						  <div class="form-group">
						    <label for="filter_detail" class="col-sm-4 control-label">Get detail content by</label>
						    <div class="col-sm-8">
						      <select id="filter_detail" id="filter_detail" class="form-control" style="width: 200px;">
						      	<option value="">None</option>
						      	<option value="DOM" <?php echo ($rowConfig['detail_filter_type'] == 'DOM') ? 'selected' : '' ?>>DOM</option>
						      	<option value="REGX" <?php echo ($rowConfig['detail_filter_type'] == 'REGX') ? 'selected' : '' ?>>Regular Expression</option>
						      </select>
						    </div>
						  </div>

					      <div class="form-group" id="frm-filter-detail">
					    	<label for="filter_content_string" class="col-sm-4 control-label">Filter string</label>
						    <div class="col-sm-8">
						      <input type="text" class="form-control" id="filter_content_string" value="<?php echo base64_decode($rowConfig['filter_string']); ?>" placeholder="DOM: div#content, Regx: <tag>(.*?)</tag>">
						    </div>
					      </div>
					      <!--/ Filter Detail -->

						  <div class="form-group">
						    <label for="limit_url" class="col-sm-4 control-label">Limit URL</label>
						    <div class="col-sm-8">
						      <input type="text" class="form-control" id="limit_url" value="<?php echo $rowConfig['limit']; ?>" placeholder="Enter a number">
						    </div>
						  </div>
						  <hr />
						  <div class="form-group">
						    <label for="dom_remove" class="col-sm-4 control-label">DOM selector removable</label>
						    <div class="col-sm-8">
						      <input type="text" class="form-control" id="dom_remove" name="dom_remove" value="<?php echo $rowConfig['dom_remove']; ?>" placeholder="Each selector separate by a comma">
						    </div>
						  </div>
						  <hr />
						  <div class="form-group">
						    <div class="col-sm-offset-4 col-sm-8">
						      <button type="button" id="btn-checkcfg" data-toggle="modal" data-target="#mymodal" class="btn btn-default"><span class="glyphicon glyphicon-check"></span> Check config</button>
						      <button type="submit" id="btn-save" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
						    </div>
						  </div>
						</form>
						<!-- Modal -->
						<div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
						  <div class="modal-dialog">
						    <div class="modal-content">
						      <div class="modal-header">
						        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						        <h4 class="modal-title" id="modal-title">&nbsp;</h4>
						      </div>
						      <div class="modal-body" id="modal-body">

						      </div>
						      <div class="modal-footer">
						        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
						      </div>
						    </div>
						  </div>
						</div>
						</div>
						<!--/ Modal -->

					</div>
					<!--/ Primary content -->


				</div>
			</div>
		</div>
	</body>
</html>